<template>
  <div class="consultation">
    <h2>评论管理</h2>
    <div class="consultation-block">
      <div class="table" v-loading="tableLoading">
        <el-table size="mini" :data="tableDataFilter" style="width: 100%">
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column prop="username" label="发表人"> </el-table-column>
          <el-table-column prop="commentContent" label="内容"> </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-button
                type="danger"
                size="mini"
                v-if="scope.row.username !== 'admin'"
                @click="deleteData(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page.sync="pageNum"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="isVisible"
      width="50%"
      :before-close="handleClose"
    >
      <el-form
        :model="form"
        status-icon
        ref="form"
        label-width="120px"
        class="demo-form"
      >
        <el-form-item label="名称" prop="squareName">
          <el-input type="text" v-model="form.squareName"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="squareContent">
          <el-input type="textarea" :rows="4" v-model="form.squareContent"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer" v-if="!form.examine">
        <el-button type="danger" @click="doSH('false')">驳 回</el-button>
        <el-button type="primary" @click="doSH('true')">通 过</el-button>
      </span>
      <span slot="footer" class="dialog-footer" v-if="form.examine">
        <el-button type="primary" @click="submitForm('form')">修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getComment, deleteComment, updateComment } from "../service.js";
export default {
  name: "home",
  mounted() {
    this.refreshTable();
  },
  data() {
    return {
      tableData: [],
      tableDataFilter: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tableLoading: false,
      isVisible: false,
      dialogTitle: "",
      form: {
        squareName: "",
        squareContent: "",
      },
    };
  },
  methods: {
    naviData() {
      this.tableDataFilter = this.tableData.slice(
        (this.pageNum - 1) * this.pageSize,
        this.pageSize * this.pageNum
      );
    },
    handleAvatarSuccess(info) {
      this.form.pics.push(info.url);
    },
    addUser() {
      this.isVisible = true;
      this.form = {
        username: "",
        pass: "",
        checkPass: "",
        title: "",
        column: "",
      };
      this.dialogTitle = "新建用户";
    },
    handleClose() {
      this.isVisible = false;
    },
    handleCurrentChange(index) {
      this.pageNum = index
      this.naviData()
    },
    doSH(examine) {
      updateComment(this.form._id, {
        examine
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success("操作");
          this.refreshTable();
          this.isVisible = false;
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateComment(this.form._id, {
            ...this.form
          }).then((res) => {
            if (res.code === 200) {
              this.$message.success("操作成功");
              this.refreshTable();
              this.isVisible = false;
            }
          });
        } else {
          return false;
        }
      });
    },
    addData() {
      this.dialogTitle = "新增";
      this.isVisible = true;
    },
    refreshTable() {
      getComment().then((res) => {
        for (let item of res.data) {
          item.userInfo = JSON.parse(item.userInfo)
          item.username = item.userInfo.username
        }
        this.tableData = res.code === 200 ? res.data : [];
        this.total = this.tableData.length;
        this.naviData()
      });
    },
    updateData(row) {
      this.form = {
        ...row
      };
      this.dialogTitle = "修改";
      this.isVisible = true;
    },
    formatEx(row) {
      if (row.examine) {
        if (row.examine === 'true') {
          return '审核通过'
        } else {
          return '审核未通过'
        }
      } else {
        return '未审核'
      }
    },
    deleteData(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteComment(row._id).then((res) => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              if (this.tableDataFilter.length == 1) {
                this.pageNum--
              }
              this.refreshTable();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped>
h2 {
  text-align: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.consultation-block {
  width: 90%;
  margin-left: 5%;
}

.pagination {
  text-align: center;
  margin-top: 20px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 60px;
  height: 60px;
  line-height: 60px;
  border: 1px dashed gray;
  text-align: center;
}
.avatar {
  width: 60px;
  height: 60px;
  display: block;
}
.img-blocks {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.el-icon-circle-close {
  position: absolute;
  top: -5px;
  right: -5px;
  cursor: pointer;
  color: red;
}
</style>
